package com.uenpay.im.ui.old.activity.search;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.listener.OnHttpRequestTextListener;
import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.net.HttpRequestAsyncTaskQueue;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.qiyunxin.android.http.net.HttpTextAsyncTask;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.weight.listview.RefreshListView;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 * @created 2016-1-23 下午2:08:00
 * @copyRight http://www.qiyunxin.com
 * @function 搜索结果
 */
public class SearchResultListActivity extends BaseActivity implements
        RefreshListView.IRefreshListViewListener {

    private int page = 1;
    private int sex = 1;
    private RefreshListView mListView;
    private TextView no_data;
    private String martialart, faction, dans, school, area;
    private SearchResultAdapter mSearchResultAdapter = null;
    private View loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_xlist_layout;
    }

    @Override
    public void initView(View view) {
        sex = getIntent().getIntExtra("sex", sex);
        martialart = getIntent().getStringExtra("martialart");
        faction = getIntent().getStringExtra("faction");
        dans = getIntent().getStringExtra("dans");
        school = getIntent().getStringExtra("school");
        area = getIntent().getStringExtra("area");
        initView();
        initListener();
        loading.setVisibility(View.VISIBLE);
        getData();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview))
                .setText(R.string.search_result);
        loading = findViewById(R.id.loading);
        mListView = (RefreshListView) findViewById(R.id.discover_listview);
        no_data = (TextView) findViewById(R.id.no_data_tv);
    }

    protected void initListener() {
        mListView.setPullLoadEnable(true);
        mListView.setRefreshListViewListener(this);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                QYXUserEntity mQYXUserEntity = (QYXUserEntity) arg0
                        .getAdapter().getItem(arg2);
                if (mQYXUserEntity != null) {
                    ActivityHandle.startUserDetailActivity(
                            SearchResultListActivity.this, mQYXUserEntity.custid);
                }
            }
        });
    }

    private void getData() {
        if (!TextUtils.isEmpty(area) && area.indexOf("-") > 0) {
            String[] areaStrs = area.split("-");
            if (areaStrs.length > 1) {
                area = areaStrs[0];
            }
        }
        SearchPeopleInvokeItem item = new SearchPeopleInvokeItem(page, sex,
                area);
        HttpRequestAsyncTaskQueue
                .getInstance()
                .AddTask(
                        new HttpTextAsyncTask(item)
                                .SetRequestType(HttpRequestType.SERVER_FAIL_AND_CACHE),
                        new OnHttpRequestTextListener() {

                            @Override
                            public void OnFail(boolean isCache, String reason) {
                                loading.setVisibility(View.GONE);
                                onLoad();
                            }

                            @Override
                            public void OnProgress(long total_bytes,
                                                   long downloaded_bytes) {
                            }

                            @Override
                            public void OnSuccess(HttpInvokeItem item,
                                                  boolean isCache, String cacheFilePath) {
                                onLoad();
                                loading.setVisibility(View.GONE);
                                SearchPeopleInvokeItem.SearchPeopleInvokeItemResult result = ((SearchPeopleInvokeItem) item)
                                        .getOutPut();
                                if (result != null) {
                                    if (page == 1) {
                                        if (result.arrayList == null
                                                || result.arrayList.size() < 1) {
                                            no_data.setVisibility(View.VISIBLE);
                                            mListView.setVisibility(View.GONE);
                                        } else {
                                            if (mSearchResultAdapter == null) {
                                                mSearchResultAdapter = new SearchResultAdapter(
                                                        SearchResultListActivity.this,
                                                        result.arrayList);
                                            } else {
                                                mSearchResultAdapter
                                                        .setData(result.arrayList);
                                            }
                                            no_data.setVisibility(View.GONE);
                                            mListView
                                                    .setVisibility(View.VISIBLE);
                                            mListView
                                                    .setAdapter(mSearchResultAdapter);
                                        }
                                        mListView.setPullLoadEnable(true);
                                    } else {
                                        if (result.arrayList != null
                                                && result.arrayList.size() > 0) {
                                            mSearchResultAdapter
                                                    .addData(result.arrayList);
                                        } else {
                                            mListView.setPullLoadEnable(false);
                                        }
                                    }
                                }
                            }
                        });

    }

    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }
}
